(define (fast-expt b n)
  (define (expt2 x) (* x x))
  (define (fast-expt-iter a count n-iter legency)
    (cond ((or (= 1 n-iter) (= count n-iter)) (* a legency))
          ((<= (* 2 count) n-iter) (fast-expt-iter (expt2 a) (* 2 count) n-iter legency))
          (else (fast-expt-iter b 1 (- n-iter count) (* a legency))))) 
  (cond ((= 0 n) 1) 
        ((= 1 n) b)
        (else (fast-expt-iter b 1 n 1))))
